package com.sale.salesystem.service.impl;

import com.sale.salesystem.entity.DashboardPermission;
import com.sale.salesystem.entity.SysUser;
import com.sale.salesystem.mapper.DashboardPermissionMapper;
import com.sale.salesystem.mapper.UserMapper;
import com.sale.salesystem.service.PermissionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class PermissionServiceImpl implements PermissionService {

    private final UserMapper userMapper;
    private final DashboardPermissionMapper permissionMapper;

    @Override
    public boolean hasPermission(Long userId, String dashboardType) {
        // 如果是管理员，直接返回true
        if (isAdmin(userId)) {
            return true;
        }

        // 查询用户是否有该仪表板的访问权限
        return permissionMapper.existsByVisitorIdAndDashboardType(userId, dashboardType);
    }

    @Override
    public boolean isAdmin(Long userId) {
        SysUser user = userMapper.selectById(userId);
        return user != null && "ADMIN".equals(user.getRole());
    }
} 